<?php

/**
 * desc:console基类
 * Created by cdm.
 * Date: 2018/4/10
 * Time: 10:25
 */

namespace app\common\console;

use think\console\Command as ThinkCommand;
use think\Config;
use think\Log;

class Command extends ThinkCommand {

    public function __construct($name = null) {
        parent::__construct($name);
        Config::load(APP_PATH . 'config/console/config.php');
        //$this->_addConsoleStartForLog();
    }

    protected function addLogStartForConsole($model = '') {
        //收到请求时间,精确到毫秒级,格式为 yyyy-mm-dd hh:mm:ss.zzz
        list($usec, $sec) = explode(" ", microtime());
        $log['request_time'] = date('Y-m-d H:i:s.') . substr($usec, 2, 3);
        //请求唯一ID,用于定位请求链路
        $log['request_id'] = $this->_createSerializeId('console');
        //项目名称或者业务名称，用于区分不同业务，业务方接入前事先与运维平台沟通并分配
        $log['project'] = 'adadmin';
        //接口模块标识
        $log['model'] = $model;
        //接口协议标识
        $log['protocol'] = 'console';
        //接口请求地址
        $log['request_url'] = $this->getName();
        //请求参数
        $log['parameter'] = "";
        //请求头
        $log['header'] = "";
        //客户端User-Agent信息
        $log['device'] = "";
        //接口请求用户标识
        $log['customerid'] = "";
        //接口请求商家code标识
        $log['companycode'] = "-";
        //客户端ip
        $log['client_ip'] = '192.168.10.54';

        Log::record($log, 'elk');
    }

    protected function addLogEndForConsole() {
        $elk = Log::getLog('elk');
        //扩展字段，存储自定义数据
        $elkMoreInfo = Log::getLog('elk_more_info');

        $elk['more_info'] = $elkMoreInfo ? $elkMoreInfo : [];
        //接口响应时间，单位为秒，精确到毫秒
        list($usec, $sec) = explode(" ", microtime());

        $endTime              = date('Y-m-d H:i:s.') . substr($usec, 2, 3);
        $usec                 = substr($elk['request_time'], -3, 3);
        $elk['response_time'] = round(microtime(true) - floatval(strtotime($elk['request_time']) . '.' . $usec), 5);

        //是ajax请求并且返回值是json格式才记录返回值，不然会把html模版都记录进去
        //为了兼容接口 改成 是json格式接存储日志
        $responseContent = "";
        //用于标识日志类型，默认填写"km"
        $elk['label'] = 'km';
        //用于标识日志类型，默认填写"km"，否则填写自定义的标签，该标签需要与运维平台事先沟通，避免冲突
        $status = config('elk_status');
        if($status) {
            $elk['status'] = intval($status);
        } else {
            $elk['status'] = 0;
        }

        Log::record($elk, 'elk');
        Log::save();
    }

    /**
     * 创建唯一标识
     * @param string $prefix 请求标识前缀
     * @return string 请求标识
     */
    private function _createSerializeId($prefix = null) {
        $uuid = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "";
        $uuid .= isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : "";
        if(isset($_SERVER['HTTP_USER_AGENT'])) {
            $uuid .= $_SERVER['HTTP_USER_AGENT'];
        }
        $uuid .= isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : "";

        return hash('ripemd128', uniqid('', true) . md5($prefix . $uuid));
    }

}